package bean;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Field;

/**
 * @author wanghao7@asiainfo-linkage.com
 * @created on 2012-7-25 下午9:12:04
 * @version
 * @since jdk1.5
 * @description
 */
public class BaseBean implements Serializable, Cloneable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7376908396322337103L;

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		Field[] fields = this.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			boolean isAccessible = field.isAccessible();
			field.setAccessible(true);
			try {
				Object value = field.get(this);
				sb.append(field.getName()).append("=").append(value).append(", ");
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
			field.setAccessible(isAccessible);
		}
		sb.append("]");
		return sb.toString();
	}

	public Object clone() {
		try {
			ByteArrayOutputStream bo = new ByteArrayOutputStream();
			ObjectOutputStream oo = new ObjectOutputStream(bo);
			oo.writeObject(this);
			ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
			ObjectInputStream oi = new ObjectInputStream(bi);
			return (oi.readObject());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
